﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public static class MachSacDAO
    {
        public static databaseDataContext _db = new databaseDataContext();

        //Hàm này lấy danh sách mạch sạc phù hợp với cường độ dòng điện
        public static object lay_danh_sach_mach_sac(double _cd) {
            var query = from ms in _db.MACHSACDIENs
                        where Convert.ToDouble(ms.Ample) >= _cd
                        select ms;
            return query.ToList();
        }

        public static List<SanPham_MachSacDTO> search_machsac(string text)
        {
            List<SanPham_MachSacDTO> listmSac = new List<SanPham_MachSacDTO>();

            var query = from p in _db.MACHSACDIENs select p;

            int stt = 1;
            foreach (var item in query)
            {
                if (item.HieuSuat1.Contains(text) || item.HieuSuat2.Contains(text) || item.Ample.Contains(text) || item.GiaTien.Value.ToString() == text)
                {
                    SanPham_MachSacDTO mSac = new SanPham_MachSacDTO();
                    mSac.stt = stt;
                    mSac.id = item.MaMachDien;
                    mSac.hieusuat1 = item.HieuSuat1;
                    mSac.hieusuat2 = item.HieuSuat2;
                    mSac.ample = item.Ample;
                    mSac.giatien = item.GiaTien.HasValue ? float.Parse(item.GiaTien.Value.ToString()) : 0;

                    //pin.chinhsua = "Chỉnh sửa";
                    //pin.xoa = "xóa";

                    listmSac.Add(mSac); stt++;
                }
            }

            return listmSac;
        }

        public static void Insert_sacpham_machsac(SanPham_MachSacDTO mSac)
        {
            MACHSACDIEN _mSac = new MACHSACDIEN();
            _mSac.HieuSuat1 = mSac.hieusuat1;
            _mSac.HieuSuat2 = mSac.hieusuat2;
            _mSac.Ample = mSac.ample;
            _mSac.GiaTien = mSac.giatien;

            _db.MACHSACDIENs.InsertOnSubmit(_mSac);
            _db.SubmitChanges();
        }

        public static void Delete_sanpham_machsac(int id)
        {
            var mach = _db.MACHSACDIENs.Single(p => p.MaMachDien == id);
            _db.MACHSACDIENs.DeleteOnSubmit(mach);
            _db.SubmitChanges();
        }

        public static SanPham_MachSacDTO load_machsac_id(int id)
        {
            SanPham_MachSacDTO mSac = new SanPham_MachSacDTO();
            var item = _db.MACHSACDIENs.Single(p => p.MaMachDien == id);

            mSac.hieusuat1 = item.HieuSuat1;
            mSac.hieusuat2 = item.HieuSuat2;
            mSac.ample = item.Ample;
            mSac.giatien = item.GiaTien.HasValue ? int.Parse(item.GiaTien.Value.ToString()) : 0;

            return mSac;
        }

        public static void Update_sanpham_machsac(SanPham_MachSacDTO mSac)
        {
            var item = _db.MACHSACDIENs.Single(p => p.MaMachDien == mSac.id);
            item.HieuSuat1 = mSac.hieusuat1;
            item.HieuSuat2 = mSac.hieusuat2;
            item.Ample = mSac.ample;
            item.GiaTien = mSac.giatien;

            _db.SubmitChanges();
        }






















    }
}
